package ObjectCreation
{
	import com.cheezeworld.GameFactory;
	import com.cheezeworld.entity.GameWorld;
	import com.cheezeworld.entity.MovingEntity;
	import com.cheezeworld.entity.MovingEntityParams;
	import com.cheezeworld.math.Vector2D;
	import com.cheezeworld.rendering.GameWorldRenderer;
	import com.cheezeworld.screens.AScreen;
	import com.cheezeworld.screens.IScreenItem;
	import com.cheezeworld.utils.Utils;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	public class MainScreen extends AScreen
	{
		public function MainScreen(a_parentScreen:IScreenItem=null)
		{
			super(a_parentScreen);
			
			m_gameWorld = new GameWorld( new Vector2D( 550, 400 ), new Vector2D( 550, 400 ) );
			new GameWorldRenderer( m_gameWorld, this );
			GameFactory.instance.addGlobalForce( new Vector2D( 0, 10 ), "gravity" );
			
			new EntityTypes();
			createUI();
			spawnBall();
		}
		
		public override function update(a_timePassed:int):void
		{
			m_gameWorld.update( a_timePassed );
		}
		
		// -- PRIVATE --
		
		private function spawnBall() : void
		{
			var entities:Array = m_gameWorld.getChildren();
			for( var i:int = 0; i < entities.length; i++ )
			{
				if( entities[ i ].type == "BouncyBall" )
				{
					m_gameWorld.removeChild( entities[ i ].id );
				}
			}
			
			var entity:MovingEntity = GameFactory.instance.getEntity( "BouncyBall", m_gameWorld ) as MovingEntity;
			entity.newPos.Set( 300, 30 );
		}
		
		private function onClick( e:MouseEvent ) : void
		{
			var params:MovingEntityParams = GameFactory.instance.getEntityParams( "BouncyBall" ) as MovingEntityParams;
			params.rendererData.boundsColor = uint( m_boundsColorTF.text );
			params.radius = uint( m_radiusTF.text );
			params.friction = Number( m_frictionTF.text );
			
			spawnBall();
		}
		
		private function createUI() : void
		{
			var bdtf:TextField = Utils.createTextField( this, 15, 15 );
			bdtf.selectable = false;
			bdtf.htmlText = Utils.createHtmlText( "Bounds Color: ", "#FFFFFF" );
			m_boundsColorTF = Utils.createTextField( this, 5 + bdtf.width, 15, true );
			m_boundsColorTF.htmlText = Utils.createHtmlText( "0xFF0000", "#FFFFFF" );
			
			var rdtf:TextField = Utils.createTextField( this, 15, 30 );
			rdtf.selectable = false;
			rdtf.htmlText = Utils.createHtmlText( "Radius: ", "#FFFFFF" );
			m_radiusTF = Utils.createTextField( this, 5 + rdtf.width, 30, true );
			m_radiusTF.htmlText = Utils.createHtmlText( "15", "#FFFFFF" );
			
			var fdtf:TextField = Utils.createTextField( this, 15, 45 );
			fdtf.selectable = false;
			fdtf.htmlText = Utils.createHtmlText( "Friction: ", "#FFFFFF" );
			m_frictionTF = Utils.createTextField( this, 5 + fdtf.width, 45, true );
			m_frictionTF.htmlText = Utils.createHtmlText( "0.8", "#FFFFFF" );
			
			var button:Sprite = new Sprite();
			button.buttonMode = true;
			addChild( button );
			button.graphics.beginFill( 0x0033FF );
			button.graphics.drawRect( 0, 0, 75, 18 );
			button.x = 20;
			button.y = 60;
			button.addEventListener( MouseEvent.CLICK, onClick );
			var buttontext:TextField = Utils.createTextField( this, 15, 60 );
			buttontext.selectable = false;
			buttontext.mouseEnabled = false;
			buttontext.htmlText = Utils.createHtmlText( "Apply Changes ", "#FFFFFF" );
		}
		
		private var m_ballParams:MovingEntityParams;
		private var m_boundsColorTF:TextField;
		private var m_radiusTF:TextField;
		private var m_frictionTF:TextField;
		private var m_gameWorld:GameWorld;
	}
}